TailsConfig.registerFormat({
  name: 'hreview',
  topNodeClass: 'hreview',

  simpleValueClasses: ["description", "summary", "type", "version", "dtreviewed"],
  urlValueClasses: ["url"],

  dateValueClasses: ["dtreviewed"],

  genericLabelAttributes: ["summary"],
  genericURLAttributes: ["url"],

  customBindings: {
    item: function(node, object) {
      if (this.html.isClass(node, "vcard")) {
        object.item_hcard = TailsConfig.getFormat("hcard").parseObject(node);

      } else if (this.html.isClass(node, "vevent")) {
        object.item_hcalendar = TailsConfig.getFormat("hcalendar").parseObject(node);

      } else {
        if (object.item) return true;
        var format = FormatBase.createFormat({
          simpleValueClasses: ["fn"],
          urlValueClasses: ["url", "photo"]});

        var item = format.parseObject(node);
        if (!item.fn && !item.photo && !item.url) item.value = FormatBase.toText(node);
        object.item = item;
      }
      return true;
    },

    reviewer: function(node, object) {
      if (object.reviewer) return true;
      object.reviewer = TailsConfig.getFormat("hcard").parseObject(node);
      return true;
    },

    rating: function(node, object) {
      if (object.rating) return true;

      var val = FormatBase.toText(node);
      var rating = null;
      if (val.match(/^\s*(\d+)\s*$/)) {
        rating = { best: 5, value: Number(RegExp.$1) };

      } else if (val.match(/^\s*(\d+)\/(\d+)\s*$/)) {
        rating = { value: Number(RegExp.$1), best: Number(RegExp.$2) };

      } else {
        var format = FormatBase.createFormat({
            simpleValueClasses: ["best", "value"]});

        var rating = format.parseObject(node);
        if (!rating.value) rating = null;
        if (!rating.best) rating.best = 5;
      }
      object.rating = rating;
    }
  },

  _getLabel: function(object) {
    return object.item? object.item.fn : null;
  },

  _getDescription: function(object) {
    var s = "";
    if (object.rating) {
      if (object.rating.best == 5) {
        var count = Number(object.rating.value);
        for (var i=0; i<count; i++) {
          s += TailsNS.getMessage("hreview_star_img");
        }
      } else {
        s += TailsNS.getMessage("hreview_desc_rating", [object.rating.value, object.rating.best]);
      }
    }

    if (object.reviewer) {
      if (s != "") s += "&nbsp;&nbsp;&nbsp;";
        s += TailsNS.getMessage("hreview_desc_reviewer", [object.reviewer.generic.label]);
    }
    return s;
  },

  end: null
});
